﻿using SharPy.GameSettings;
using SharPy.Parser;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.UI.Xaml;

namespace SharPy.GameObjects
{
    class clMusic
    {
        static Windows.Storage.StorageFolder folder = SharPy.Parser.clText.gameFolder;

        internal static async Task<string> playLine(string sCurLine)
        {
            MatchCollection mc = Regex.Matches(sCurLine, @"(play)\s(music|sound|voice)\s""(.*)""");
            
            string path = folder.Path + "\\" +mc[0].Groups[3].Value;
            //path = path.Replace('\\', '/');

            if(Regex.IsMatch(path, @"\w.mp3"))
            {
                return path;
            }
            else
            {
                return path;
            }
        }

        internal static bool RegExpCheck(string s)
        {
            if (Regex.IsMatch(s, "^play .*"))
                return true;
            if (Regex.IsMatch(s, "^stop .*"))
                return true;
            return false;
        }

        internal static async Task<clCommand> Parse(string s)
        {
            if (Regex.IsMatch(s, "^play .*"))
                return (new clCommand(clCommand.Name.PlayMusic
                                    , await playLine(s)
                        ));
            if (Regex.IsMatch(s, "^stop .*"))
                return (new clCommand(clCommand.Name.StopMusic
                                        , null
                                        , null
                        ));
            throw new ArgumentException();
        }

        internal static async Task<int> CommandHandle(clCommand pCom, GamePage page)
        {
            Windows.UI.Xaml.Controls.MediaElement bm = page.FindName("backgroundMusic") as Windows.UI.Xaml.Controls.MediaElement;                    
            switch (pCom.sName)
            {
                case clCommand.Name.PlayMusic:
                    if (pCom.sParameter[0] != null)
                    {
                        Uri packUrM = new Uri(pCom.sParameter[0]);
                        bm.Source = packUrM;
                        PlayMedia(bm, page);
                    }
                    return 0;
                case clCommand.Name.StopMusic:
                    PauseMedia(bm, page);
                    return 0;
            }
            return 1;
        }

        internal async static void PlayMedia(Windows.UI.Xaml.Controls.MediaElement pbm, GamePage page)
        {
            await page.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                pbm.Play();
            });
        }

        internal async static void PauseMedia(Windows.UI.Xaml.Controls.MediaElement pbm, GamePage page)
        {
            await page.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                pbm.Pause();
            });
        }
       
    }
}
